package com.crocodileQ.blog.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.crocodileQ.blog.domain.BlogRole;
import com.crocodileQ.blog.domain.BlogUserRole;
import com.crocodileQ.blog.domain.dto.BlogUserDto;
import com.crocodileQ.blog.domain.vo.BlogUserVo;
import com.crocodileQ.blog.mapper.BlogRoleMapper;
import com.crocodileQ.common.constant.BlogConstants;
import com.crocodileQ.common.core.domain.entity.SysUser;
import com.crocodileQ.common.core.domain.model.LoginUser;
import com.crocodileQ.common.utils.DateUtils;
import com.crocodileQ.common.utils.GenerateContextUtil;
import com.crocodileQ.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.crocodileQ.blog.mapper.BlogUserMapper;
import com.crocodileQ.blog.domain.BlogUser;
import com.crocodileQ.blog.service.IBlogUserService;
import org.springframework.transaction.annotation.Transactional;

import javax.security.auth.callback.PasswordCallback;

/**
 * 用户管理Service业务层处理
 * 
 * @author crocodilePole
 * @date 2025-04-03
 */
@Service
public class BlogUserServiceImpl implements IBlogUserService 
{
    @Autowired
    private BlogUserMapper blogUserMapper;

    @Autowired
    private BlogRoleServiceImpl blogRoleService;

    @Autowired
    private BlogUserRoleServiceImpl blogUserRoleService;

    /**
     * 查询用户管理
     * 
     * @param id 用户管理主键
     * @return 用户管理
     */
    @Override
    public BlogUserVo selectBlogUserById(Long id)
    {
        return blogUserMapper.selectBlogUserById(id);
    }

    /**
     * 查询用户管理列表
     * 
     * @param blogUser 用户管理
     * @return 用户管理
     */
    @Override
    public List<BlogUserVo> selectBlogUserList(BlogUser blogUser)
    {
        List<BlogUserVo> blogUserVos = blogUserMapper.selectBlogUserList(blogUser);

        return blogUserVos ;
    }

    /**
     * 新增用户管理
     * 
     * @param blogUser 用户管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertBlogUser(BlogUser blogUser)
    {
        //默认密码：123456
        blogUser.setPassword(SecurityUtils.encryptPassword(BlogConstants.DEFAULT_PASSWORD));
        //随机生成用户名 + 当前时间戳 (有概率重复：后续使用redis 自增完善）
        String randomName = GenerateContextUtil.generateUserName();
        blogUser.setUserName(randomName);

        //账户状态默认正常
        blogUser.setStatus(BlogConstants.BLOG_USER_STATUS_NORMAL);
        //创建人 获取用户id
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        blogUser.setCreateBy(user.getUserId().toString());
        //头像
        blogUser.setAvatar(BlogConstants.BLOG_USER_DEFAULT_AVATAR);
        blogUser.setCreateTime(DateUtils.getNowDate());

        int result = blogUserMapper.insertBlogUser(blogUser);

        //账户角色设置 多表设置
        BlogRole blogRole = blogRoleService.selectBlogRoleByKey(BlogConstants.BLOG_USER_DEFAULT_ROLE);
        //根据中间表设置映射关系
        BlogUserRole blogUserRole = new BlogUserRole();
        blogUserRole.setUserId(blogUser.getId());
        blogUserRole.setRoleId(blogRole.getId());

        blogUserRoleService.insertBlogUserRole(blogUserRole);

        return result;
    }

    /**
     * 修改用户管理
     * 
     * @param blogUser 用户管理
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateBlogUser(BlogUserDto blogUser)
    {

        blogUser.setUpdateTime(DateUtils.getNowDate());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        blogUser.setUpdateBy(user.getUserId().toString());

        int result = blogUserMapper.updateBlogUser(blogUser);
        //同步更新role信息
        Long userId = blogUser.getId();
        blogUserRoleService.deleteBlogUserRoleById(userId);
        //批量插入
        ArrayList<Long> roleIds = blogUser.getRoleIds();
        blogUserRoleService.BatchInsertBlogUserRole(userId,roleIds);

        return result;
    }

    /**
     * 批量删除用户管理
     * 
     * @param ids 需要删除的用户管理主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    public int deleteBlogUserByIds(Long[] ids)
    {
        //1、中间表，删除用户于角色的关联信息
        blogUserRoleService.deleteBlogUserRoleByUserIds(ids);

        //2、删除用户信息
        return blogUserMapper.deleteBlogUserByIds(ids);
    }

    /**
     * 删除用户管理信息
     * 
     * @param id 用户管理主键
     * @return 结果
     */
    @Override
    public int deleteBlogUserById(Long id)
    {
        return blogUserMapper.deleteBlogUserById(id);
    }

    /**
     * 重置用户密码
     * @param ids
     * @return
     */
    public int resetPwdByIds(Long[] ids) {
        //默认密码
        String defaultPwd = SecurityUtils.encryptPassword(BlogConstants.DEFAULT_PASSWORD);
        return blogUserMapper.resetPwdByIds(defaultPwd,ids);
    }
}
